home *** CD-ROM | disk | FTP | other *** search
/ ShareWare OnLine 2 / ShareWare OnLine Volume 2 (CMS Software)(1993).iso / os2 / memsz161.zip / DEBUG.C < prev    next >
C/C++ Source or Header  |  1993-02-18  |  6KB  |  206 lines

  1. /******************************************************************** DEBUG.C
  2.  *                                        *
  3.  *  Debugging Aids                                *
  4.  *                                        *
  5.  ****************************************************************************/
  6.  
  7. #define INCL_WIN
  8. #include <os2.h>
  9.  
  10. #include <stdarg.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13.  
  14. #include "debug.h"
  15.  
  16. extern HFILE Timer = 0 ;
  17. extern BOOL Trace = FALSE ;
  18.  
  19.  
  20. /****************************************************************************
  21.  *                                        *
  22.  *             Display Debug Message                    *
  23.  *                                        *
  24.  ****************************************************************************/
  25.  
  26. extern VOID Debug ( HWND hwnd, char *Message, ... )
  27. {
  28.  /***************************************************************************
  29.   * Local Declarations                                *
  30.   ***************************************************************************/
  31.  
  32.   va_list Marker ;
  33.   char Text [500] ;
  34.  
  35.  /***************************************************************************
  36.   * Format the debug message.                            *
  37.   ***************************************************************************/
  38.  
  39.   va_start ( Marker, Message ) ;
  40.   vsprintf ( Text, Message, Marker ) ;
  41.   va_end ( Marker ) ;
  42.  
  43.  /***************************************************************************
  44.   * Display the log message and wait for the user to press ENTER.        *
  45.   ***************************************************************************/
  46.  
  47.   WinMessageBox ( HWND_DESKTOP, hwnd, Text, "Debug", 0, MB_ENTER ) ;
  48. }
  49.  
  50. /****************************************************************************
  51.  *                                        *
  52.  *               Log Debug Message                    *
  53.  *                                        *
  54.  ****************************************************************************/
  55.  
  56. extern VOID Log ( char *Message, ... )
  57. {
  58.  /***************************************************************************
  59.   * Local Declarations                                *
  60.   ***************************************************************************/
  61.  
  62.   FILE *Log ;
  63.   va_list Marker ;
  64.  
  65.  /***************************************************************************
  66.   * Try to open the log file.  If unsuccessful, just return.            *
  67.   ***************************************************************************/
  68.  
  69.   Log = fopen ( "LOG", "at" ) ;
  70.  
  71.   if ( Log == NULL )
  72.     return ;
  73.  
  74.  /***************************************************************************
  75.   * Format and write the message to the log file.                *
  76.   ***************************************************************************/
  77.  
  78.   va_start ( Marker, Message ) ;
  79.   vfprintf ( Log, Message, Marker ) ;
  80.   va_end ( Marker ) ;
  81.  
  82.  /***************************************************************************
  83.   * Close the log file and return.                        *
  84.   ***************************************************************************/
  85.  
  86.   fclose ( Log ) ;
  87. }
  88.  
  89. /****************************************************************************
  90.  *                                        *
  91.  *                Open Timer for Use                    *
  92.  *                                        *
  93.  ****************************************************************************/
  94.  
  95. extern BOOL OpenTimer ( VOID )
  96. {
  97.   USHORT Action ;
  98.  
  99.   if ( Timer )
  100.     DosClose ( Timer ) ;
  101.  
  102.   if ( DosOpen ( "TIMER$", &Timer, &Action, 0, FILE_NORMAL, FILE_OPEN, OPEN_SHARE_DENYNONE, 0 ) )
  103.   {
  104.     return ( FALSE ) ;
  105.   }
  106.  
  107.   return ( TRUE ) ;
  108. }
  109.  
  110. /****************************************************************************
  111.  *                                        *
  112.  *                Close Timer                    *
  113.  *                                        *
  114.  ****************************************************************************/
  115.  
  116. extern VOID CloseTimer ( VOID )
  117. {
  118.   DosClose ( Timer ) ;
  119. }
  120.  
  121. /****************************************************************************
  122.  *                                        *
  123.  *             Read Time from HRTIMER.SYS                *
  124.  *                                        *
  125.  ****************************************************************************/
  126.  
  127. extern BOOL GetTime ( PTIMESTAMP pts )
  128. {
  129.   USHORT ByteCount ;
  130.  
  131.   if ( DosRead ( Timer, pts, sizeof(*pts), &ByteCount ) )
  132.     return ( FALSE ) ;
  133.  
  134.   return ( TRUE ) ;
  135. }
  136.  
  137. /****************************************************************************
  138.  *                                        *
  139.  *               Calculate Elapsed Time                *
  140.  *                                        *
  141.  ****************************************************************************/
  142.  
  143. extern ULONG ElapsedTime ( PTIMESTAMP ptsStart, PTIMESTAMP ptsStop, PULONG pulNs )
  144. {
  145.   ULONG ulMsecs, ulNsecs;
  146.   TIMESTAMP tsStart, tsStop ;
  147.  
  148.   tsStart = *ptsStart ;               // De-reference timestamp
  149.                           //     structures for speed
  150.   tsStop  = *ptsStop ;
  151.  
  152.   ulMsecs = tsStop.ulMs - tsStart.ulMs ;      // Elapsed milliseconds
  153.  
  154.   if( tsStart.ulNs > tsStop.ulNs )          // If nanosecond overflow ...
  155.   {
  156.     ulNsecs = (1000000 + tsStop.ulNs) - tsStart.ulNs; // Adjust nanoseconds
  157.     ulMsecs--;                          // Adjust milliseconds
  158.   }
  159.   else
  160.     ulNsecs = tsStop.ulNs - tsStart.ulNs ;    // No overflow..Elapsed nanos
  161.  
  162.   *pulNs = ulNsecs ;
  163.  
  164.   return ( ulMsecs ) ;
  165. }
  166.  
  167. /****************************************************************************
  168.  *                                        *
  169.  *  Allocate Memory                                *
  170.  *                                        *
  171.  ****************************************************************************/
  172.  
  173. extern PVOID AllocateMemory ( USHORT ByteCount )
  174. {
  175.   #ifdef ALLOCATE_THROUGH_DOS
  176.   {
  177.     SEL Selector ;
  178.     DosAllocSeg ( ByteCount, &Selector, SEG_NONSHARED ) ;
  179.     return ( MAKEP ( Selector, 0 ) ) ;
  180.   }
  181.   #else
  182.   {
  183.     return ( malloc ( ByteCount ) ) ;
  184.   }
  185.   #endif
  186. }
  187.  
  188. /****************************************************************************
  189.  *                                        *
  190.  *  Free Memory                                 *
  191.  *                                        *
  192.  ****************************************************************************/
  193.  
  194. extern VOID FreeMemory ( PVOID Memory )
  195. {
  196.   #ifdef ALLOCATE_THROUGH_DOS
  197.   {
  198.     DosFreeSeg ( SELECTOROF ( Memory ) ) ;
  199.   }
  200.   #else
  201.   {
  202.     free ( Memory ) ;
  203.   }
  204.   #endif
  205. }
  206.